/**
 * @file /src/dynamic_button.c
 *
 * @brief Qt based gui for rhzd_assist.
 *
 * @date October 2025
 **/

/*****************************************************************************
** Includes
*****************************************************************************/

#include <QPushButton>
#include <QTimer>
#include <QMouseEvent>
#include <QMessageBox>

#include "dynamic_button.hpp"

DynamicButton::DynamicButton(const QString &text, QWidget *parent)
    : QPushButton(text, parent),
      pressTimer(new QTimer(this)),
      longPressThreshold(3000),
      isLongPressing(false)
{

    this->setMinimumHeight(30);
    this->setMinimumWidth(90);
    this->setMaximumHeight(30);
    this->setMaximumWidth(110);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    // 设置定时器属性
    pressTimer->setSingleShot(true);
    pressTimer->setInterval(longPressThreshold);

    // 连接信号和槽
    connect(pressTimer, &QTimer::timeout, this, &DynamicButton::onLongPress);

    // 设置按钮初始样式
    // setupButtonStyle();
}

void DynamicButton::setLongPressThreshold(int milliseconds) 
{
    longPressThreshold = milliseconds;
    pressTimer->setInterval(longPressThreshold);
}

int DynamicButton::getLongPressThreshold() const 
{
    return longPressThreshold;
}

int DynamicButton::setCmd(const QString &cmd)
{
    this->cmd = cmd;
    return 0;
}

QString DynamicButton::getCmd() const 
{
    return cmd;
}

void DynamicButton::mousePressEvent(QMouseEvent *event) 
{
    if (event->button() == Qt::LeftButton) {
        pressTimer->start();
        // setPressedStyle();
    }
    QPushButton::mousePressEvent(event);
}

void DynamicButton::mouseReleaseEvent(QMouseEvent *event) 
{
    if (event->button() == Qt::LeftButton) {
        pressTimer->stop();
        // setNormalStyle();
    }
    QPushButton::mouseReleaseEvent(event);
}

void DynamicButton::onLongPress() 
{
    isLongPressing = true;
    // 显示删除确认对话框
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this,
                                 "确认删除",
                                 "确定要删除这个按钮吗？",
                                 QMessageBox::Yes | QMessageBox::No);

    if (reply == QMessageBox::Yes) {
        // 从父布局中移除并删除自己
        Q_EMIT sigDbuttonDelete(this);
        deleteLater();
    } else {
        // 用户取消删除，恢复正常样式
        // setNormalStyle();
        isLongPressing = false;
    }
}

void DynamicButton::setupButtonStyle() 
{
    setNormalStyle();
}

void DynamicButton::setPressedStyle() 
{
    setStyleSheet(
        "DynamicButton {"
        "    background-color: #ff9800;"
        "    border: none;"
        "    color: white;"
        "    padding: 12px 24px;"
        "    text-align: center;"
        "    text-decoration: none;"
        "    font-size: 16px;"
        "    margin: 4px 2px;"
        "    border-radius: 8px;"
        "}"
    );
}

void DynamicButton::setNormalStyle() 
{
    setStyleSheet(
        "DynamicButton {"
        "    background-color: #4CAF50;"
        "    border: none;"
        "    color: white;"
        "    padding: 12px 24px;"
        "    text-align: center;"
        "    text-decoration: none;"
        "    font-size: 16px;"
        "    margin: 4px 2px;"
        "    border-radius: 8px;"
        "}"
        "DynamicButton:hover {"
        "    background-color: #45a049;"
        "}"
        "DynamicButton:pressed {"
        "    background-color: #3d8b40;"
        "}"
    );
}